
<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <title>Runtime</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=2.0">
<link rel="stylesheet", href="/./css/style.css">
<link rel="shortcut icon", href="/./favicon.ico">
<link rel="apple-touch-icon", href="/./apple-touch-icon.png">

        
    
        <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
    
    <link rel='stylesheet' href="/./css/dracula.css">

    <meta name="generator" content="Hexo 4.2.0"></head>
    <body>
        <header class="al_header al_pos_fixed">
    <div class="al_header_container dis_flex_jcenter">
        <div class="al_header_container_left">
            <div class="al_header_site_title">
                <a href="/">Kevin' Blog</a>
            </div>
        </div>

        <div class="dis_flex_jcenter">
            <div class="al_header_setting">
                <svg class="al_header_icon">
                    <use xmlns="http://www.w3.org/2000/svg" xlink:href="/assets/svg_icons.svg#svg-menu"></use>
                </svg>
            </div>
        </div>
    </div>
</header>

        <div class="al_sidebar">

    <div class="al_sidebar_overlay al_full_cover"></div>

    <div class="al_pos_fixed al_sidebar_cnt">
        <div class="dis_flex_acenter al_sidebar_header">
            <h3>Kevin' Blog</h3>
            <div class="al_sidebar_close al_header_setting">
                <svg class="al_header_icon">
                    <use xmlns="http://www.w3.org/2000/svg" xlink:href="/assets/svg_icons.svg#svg-close"></use>
                </svg>
            </div>
        </div>

        <div class="al_sidebar_author_cnt">

            <div class="al_sidebar_author_info">
                <h4>Kevin</h4>
                <img class="al_sidebar_avatar" src="https://gitee.com/zcwtr/zcwtr/raw/master/image/avatar.jpeg">
                <p>極</p>
            </div>

            
        </div>
    </div>
</div>

        
    <div class="dis_flex_center al_lightbox_cnt al_full_cover">
        <img class="al_lightbox_img"/>
    </div>
    <div class="al_page_background dis_flex_center al_full_cover"></div>
    <div class="al_page_container">
        <div class="al_pos_ab al_fake_background"></div>
        <div class="al_main_container al_main_page_container">
            <article class="al_article">
                <header>
                    <h1 class="al_page_title">
                        Runtime
                    </h1>
                    <div class="al_page_info dis_flex">
                        <div class="al_page_content_info">
                            January 04, 2021 07:01 AM
                        </div>

                        
                            <div class="al_page_content_info">
                                701 words
                            </div>
                        

                        
                            <div class="al_page_content_info">
                                2 minutes read
                            </div>
                        
                        <span class="tags"></span>
                    </div>
                </header>

                

                
                <section id="post-body">
                    <p>在Object-C中的NSObject对象中</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@interface NSObject &lt;NSObject&gt; &#123;</span><br><span class="line">#pragma clang diagnostic push</span><br><span class="line">#pragma clang diagnostic ignored &quot;-Wobjc-interface-ivars&quot;</span><br><span class="line">    Class isa  OBJC_ISA_AVAILABILITY;</span><br><span class="line">#pragma clang diagnostic pop</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述表述Objective-C类是由Class类型来表示的，它实际上是一个指向objc_class结构体的指针</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;&#x2F; An opaque type that represents an Objective-C class.</span><br><span class="line">typedef struct objc_class *Class;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;&#x2F; Represents an instance of a class.</span><br><span class="line">struct objc_object &#123;</span><br><span class="line">    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;&#x2F; A pointer to an instance of a class.</span><br><span class="line">typedef struct objc_object *id;</span><br></pre></td></tr></table></figure>
<p>由此可见可以看到Class、id 前者是类，后者指向类的指针，id是指向objc_object的一个指针，而objc_object有个isa指向objc_class的一个指针<br>So,不管id，还是Class最后指向的都是objc_class这个结构体<br>objc_class结构体中的定义如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">struct objc_class &#123;</span><br><span class="line">    Class _Nonnull isa  OBJC_ISA_AVAILABILITY;</span><br><span class="line"></span><br><span class="line">#if !__OBJC2__</span><br><span class="line">    Class _Nullable super_class                              OBJC2_UNAVAILABLE;</span><br><span class="line">    const char * _Nonnull name                               OBJC2_UNAVAILABLE;</span><br><span class="line">    long version                                             OBJC2_UNAVAILABLE;</span><br><span class="line">    long info                                                OBJC2_UNAVAILABLE;</span><br><span class="line">    long instance_size                                       OBJC2_UNAVAILABLE;</span><br><span class="line">    struct objc_ivar_list * _Nullable ivars                  OBJC2_UNAVAILABLE;</span><br><span class="line">    struct objc_method_list * _Nullable * _Nullable methodLists                    OBJC2_UNAVAILABLE;</span><br><span class="line">    struct objc_cache * _Nonnull cache                       OBJC2_UNAVAILABLE;</span><br><span class="line">    struct objc_protocol_list * _Nullable protocols          OBJC2_UNAVAILABLE;</span><br><span class="line">#endif</span><br><span class="line"></span><br><span class="line">&#125; OBJC2_UNAVAILABLE;</span><br><span class="line">&#x2F;* Use &#96;Class&#96; instead of &#96;struct objc_class *&#96; *&#x2F;</span><br></pre></td></tr></table></figure>
<p>在runtime使用当中，我们经常需要用到的字段，它们的定义</p>
<ul>
<li><p>isa Class对象，指向objc_class结构体的指针，也就是这个Class的MetaClass(元类)</p>
<p><code>-</code> 类的实例对象的 isa 指向该类;该类的 isa 指向该类的 MetaClass</p>
<p><code>-</code> MetaCalss的isa对象指向RootMetaCalss</p>
</li>
<li><p>super_class Class对象指向父类对象</p>
<p><code>-</code> 如果该类的对象已经是RootClass，那么这个super_class指向nil</p>
<p><code>-</code> MetaCalss的SuperClass指向父类的MetaCalss</p>
<p><code>-</code> MetaCalss是RootMetaCalss，那么该MetaClass的SuperClass指向该对象的RootClass</p>
</li>
<li><p>ivars 类中所有属性的列表，使用场景：我们在字典转换成模型的时候需要用到这个列表找到属性的名称，去取字典中的值，KVC赋值，或者直接Runtime赋值</p>
</li>
<li><p>methodLists 类中所有的方法的列表，类中所有方法的列表，使用场景：如在程序中写好方法，通过外部获取到方法名称字符串，然后通过这个字符串得到方法，从而达到外部控制App已知方法。</p>
</li>
<li><p>cache 主要用于缓存常用方法列表，每个类中有很多方法，我平时不用的方法也会在里面，每次运行一个方法，都要去methodLists遍历得到方法，如果类的方法不多还行，但是基本的类中都会有很多方法，这样势必会影响程序的运行效率，所以cache在这里就会被用上，当我们使用这个类的方法时先判断cache是否为空，为空从methodLists找到调用，并保存到cache，不为空先从cache中找方法，如果找不到在去methodLists，这样提高了程序方法的运行效率</p>
</li>
<li><p>protocols 故名思义，这个类中都遵守了哪些协议，使用场景：判断类是否遵守了某个协议上</p>
</li>
</ul>

                </section>

                
                
    <div id="al_gitalk_cnt">
    <script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
    <script>
        var gitalk = new Gitalk({
            clientID: "b311dfc46e6eb58aa382",
            clientSecret: "386b7ecdfdbe4da2b35281a0d168fe13284572cb",
            repo: "comment",
            owner: "ZTKevin",
            admin: "ZTKevin",
            id: "Runtime",
        });
        gitalk.render("al_gitalk_cnt")
    </script>
                


            
            <nav class="dis_flex post-nav">
                <span class="prev">
                    
                        <a href="/2021/06/19/PrivateSpec/">
                            <span class="arrow">
                                ← 
                            </span>Newer Posts
                        </a>
                    
                </span>
                <span class="next">
                    
                        <a href="/2020/12/14/NSLog%E6%89%93%E5%8D%B0%E4%B8%AD%E6%96%87%E5%8F%8A%E6%89%93%E5%8D%B0%E4%B8%8D%E5%85%A8%E7%9A%84%E5%AE%8F/">
                            Older Posts
                            <span class="arrow"> → </span>
                        </a>
                    
                </span>
            </nav>
        </div>
    </div>


        <div class="al_index_footer dis_flex_center">
    <div class="al_index_footer_item al_index_footer_title">
        Kevin
    </div>

    
    

    <div class="al_index_footer_item al_index_footer_extra">
        Created By 
        <a href="https://github.com/iGuan7u/Acetolog" target="_blank" rel="noopener">AcetoLog</a>
         · Power By 
        <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo<a>
    </div>

    <div class="al_index_footer_item al_index_footer_extra_right">
        All Right Reserved
    </div>
</div>

        <script type="text/javascript" async="async" src="/javascripts/acelog.js"></script>
        
        
        
        
    </body>
</html>
        